// pages/indexfirst/indexfirst.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getbannerurl: app.data.url +'/zhongxue/maoapi/guide',
    getUserToken: app.data.url +"/zhongxue/maoapi/grantauthorization",
    shuju:[],
    status:true,
  
  },

  start:function() {
    var xiaoqu = wx.getStorageSync('xiaoqu');
    if(xiaoqu){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }else{
      wx.navigateTo({
        url: '/pages/indexsencond/indexsencond',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.setStorageSync('user', null);
    wx.showLoading({
      title: '检测更新中',
      mask: true,
    })
    ea.toUpdata(function(){
      wx.hideLoading();
    });
    //检查是否授权，
    this.getData();
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
        }else{
          that.setData({ status:false});
        }
      }
    })
  },
getData:function(){
  var that = this;
  var user = wx.getStorageSync('user');
  wx.request({
    url: that.data.getbannerurl,
    data: { token: user.token },
    method: 'post',
    success: function (res) {
      var datas = res.data;
      console.log(datas);
      if (datas.code == 200) {
        that.setData({ shuju: datas.data });
        // setTimeout(function () {
        //   that.start();
        
        // }, 3000)
      } else {
        that.setData({ status: false });
        wx.showToast({
          title: datas.msg,
          icon: 'none',
          duration:app.data.quanbushijian
        })
      }
    }
  })
},


  getUsers:function(res){
    var that =this;
    if (res.detail.errMsg =='getUserInfo:ok'){
      wx.getUserInfo({
        success:function(res){
          console.log(res);
        }
      })
      var user = res.detail.userInfo;
      wx.login({
        success:function(ress){
          console.log(ress);
          user.code = ress.code;
          //console.log(user);
          wx.request({
            url: that.data.getUserToken,
            data:user,
            success:function(res){
              var data = res.data;
              console.log(data);
              // return
              if(data.code==200){
                if(data.data){
                  wx.setStorageSync('user', data.data)
                  that.getData();
                  that.setData({ status:true})
                }
              }else{
                wx.showToast({
                  title: data.msg,
                  icon:'none',
                  duration: app.data.quanbushijian
                })
              }
            }
          })
        }
      })
    }
  },





  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var user = wx.getStorageSync('user');
    if(!user){
      this.setData({ status:false});
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path:'/pages/indexfirst/indexfirst',
    }
  }
})